<script>
import VueGridLayout from 'vue-grid-layout'
const GridLayout = VueGridLayout.GridLayout
const GridItem = VueGridLayout.GridItem
import CustomDialog from './dialog'
import { requestGet, requestPost } from '@/api/system/common'
import ColumnItem from '@/views/portal4.0/livebos/component/item/ColumnItem.vue'
export default {
  name: 'LiveBosLayoutSetting',
  components: { GridItem, GridLayout, CustomDialog, ColumnItem },
  data() {
    return {
      dialogVisible: false,
      dataList: [],
      layoutId: ''
    }
  },
  methods: {
    openDialog(data) {
      this.layoutId = data.id
      this.initData()
      this.dialogVisible = true
    },
    initData() {
      requestGet('rest/api/sys/livebos/v1/column/default/columns',
        { lid: this.layoutId }).then(res => {
        const { data } = res
        this.dataList = data
      }).catch(() => {
        this.dataList = []
      })
    },
    closeDialog() {
      this.dialogVisible = false
    },
    resizedEvent(i, newH, newW, newHPx, newWPx) {
      this.dataList = this.dataList.map(item => {
        if (item.i === i) {
          item.w = newW
          item.h = newH
        }
        return item
      })
      this.updateEntity()
    },
    movedEvent(i, newX, newY) {
      this.dataList = this.dataList.map(item => {
        if (item.i === i) {
          item.x = newX
          item.y = newY
        }
        return item
      })
      this.updateEntity()
    },
    updateEntity() {
      requestPost('rest/api/sys/livebos/v1/column/update/all', this.dataList).then(res => {
      })
    },
    doAdd() {
      this.$nextTick(() => {
        this.$refs.custom.openDialog(this.layoutId)
      })
    }
  }
}
</script>

<template>
  <el-dialog
    :fullscreen="true"
    :show-close="false"
    title="提示"
    append-to-body
    :visible.sync="dialogVisible"
  >
    <div slot="title" class="custom-header">
      <div class="dialog-title">布局设置</div>
      <div class="dialog-button">
        <el-button type="primary" icon="el-icon-circle-plus" @click="doAdd">
          新增栏目
        </el-button>
        <el-button type="primary" @click="closeDialog">
          返回<i class="el-icon-arrow-right el-icon-caret-right" />
        </el-button>
      </div>
    </div>
    <div v-if="dataList" class="custom-content">
      <grid-layout
        :layout="dataList"
        :col-num="16"
        :row-height="40"
        :auto-size="false"
        :is-draggable="true"
        :is-resizable="true"
        :use-css-transforms="true"
        :responsive="false"
        :use-style-cursor="false"
        :vertical-compact="true"
        :margin="[15, 15]"
      >
        <grid-item
          v-for="(item) in dataList"
          :key="item.id"
          :x="item.x"
          :y="item.y"
          :w="item.w"
          :h="item.h"
          :i="item.i"
          :style="{zIndex: item.zIndex}"
          @resized="resizedEvent"
          @moved="movedEvent"
        >
          <column-item :lid="item.id" @addColumn="doAdd" @update-data="initData" />
        </grid-item>
      </grid-layout>
    </div>
    <custom-dialog ref="custom" @fetch-data="initData" />
  </el-dialog>
</template>

<style lang="scss" scoped>
.custom-header {
  width: 100%;
  height: 50px;
  display: flex;
  flex-flow: row nowrap;
  justify-content: space-between;
  align-items: center;
  padding: 0 20px;
  box-sizing: border-box;
  box-shadow: 0 2px 4px rgba(0, 0, 0, .12), 0 0 6px rgba(0, 0, 0, .04);
  .dialog-title {
    font-size: 1.1rem;
    font-weight: bold;
  }
}
.custom-content {
  position: relative;
  width: 100%;
}
::v-deep .el-dialog__header {
  padding: 0 !important;
}
</style>
